/*
 * The view containing all the others views.
 */

/*
 * PluggisView.java
 */
package edu.chalmers.se.tda367.gui.view;

import edu.chalmers.se.tda367.controller.ExerciseController;
import java.beans.PropertyChangeEvent;
import java.io.FileNotFoundException;
import org.jdesktop.application.Action;
import org.jdesktop.application.SingleFrameApplication;
import org.jdesktop.application.FrameView;
import java.beans.PropertyChangeListener;
import javax.swing.JDialog;
import javax.swing.JFrame;
import edu.chalmers.se.tda367.observer.GlobalObserverList;
import edu.chalmers.se.tda367.main.Main;
import edu.chalmers.se.tda367.main.Pluggis;
import edu.chalmers.se.tda367.model.Test;
import edu.chalmers.se.tda367.question.MathQuestionConFig;

/**
 * The application's main frame.
 */
public class MainView extends FrameView implements PropertyChangeListener {

    private ExerciseController exCon;
    private JDialog aboutBox;
    private String typeOfQuestion;
    private MathQuestionConFig config;
    private SelectAreaView areaView;
    //private ExerciseView exerciseView;

    public MainView(SingleFrameApplication app) throws FileNotFoundException {
        //Netbeanskod
        super(app);
        initComponents();

        GlobalObserverList.getInstance().addObserver(this);
        exCon = new ExerciseController();


        mainPanel.add(new LoginView());
        Pluggis.getInstance().setMainViewPanel(mainPanel);


    }

    @Action
    public void showAboutBox() {
        if (aboutBox == null) {
            JFrame mainFrame = Main.getApplication().getMainFrame();
            aboutBox = new PluggisAboutBox(mainFrame);
            aboutBox.setLocationRelativeTo(mainFrame);

        }
        Main.getApplication().show(aboutBox);
    }

    public void startExercise(String area) {
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        mainPanel = new javax.swing.JPanel();
        menuBar = new javax.swing.JMenuBar();
        javax.swing.JMenu fileMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem exitMenuItem = new javax.swing.JMenuItem();
        javax.swing.JMenu helpMenu = new javax.swing.JMenu();
        javax.swing.JMenuItem aboutMenuItem = new javax.swing.JMenuItem();

        mainPanel.setMaximumSize(new java.awt.Dimension(820, 550));
        mainPanel.setName("mainPanel"); // NOI18N
        mainPanel.setPreferredSize(new java.awt.Dimension(820, 550));
        mainPanel.setSize(new java.awt.Dimension(820, 550));

        menuBar.setName("menuBar"); // NOI18N
        menuBar.setPreferredSize(new java.awt.Dimension(820, 22));
        menuBar.setSize(new java.awt.Dimension(820, 22));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(edu.chalmers.se.tda367.main.Main.class).getContext().getResourceMap(MainView.class);
        fileMenu.setText(resourceMap.getString("fileMenu.text")); // NOI18N
        fileMenu.setName("fileMenu"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(edu.chalmers.se.tda367.main.Main.class).getContext().getActionMap(MainView.class, this);
        exitMenuItem.setAction(actionMap.get("quit")); // NOI18N
        exitMenuItem.setText(resourceMap.getString("exitMenuItem.text")); // NOI18N
        exitMenuItem.setName("exitMenuItem"); // NOI18N
        fileMenu.add(exitMenuItem);

        menuBar.add(fileMenu);

        helpMenu.setText(resourceMap.getString("helpMenu.text")); // NOI18N
        helpMenu.setName("helpMenu"); // NOI18N

        aboutMenuItem.setAction(actionMap.get("showAboutBox")); // NOI18N
        aboutMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.META_MASK));
        aboutMenuItem.setText(resourceMap.getString("aboutMenuItem.text")); // NOI18N
        aboutMenuItem.setName("aboutMenuItem"); // NOI18N
        helpMenu.add(aboutMenuItem);

        menuBar.add(helpMenu);

        setComponent(mainPanel);
        setMenuBar(menuBar);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel mainPanel;
    private javax.swing.JMenuBar menuBar;
    // End of variables declaration//GEN-END:variables

    public void propertyChange(PropertyChangeEvent pce) {
        String change = pce.getPropertyName();

        if (change.equals("GUI.RepaintView")) {
            mainPanel.revalidate();
            mainPanel.repaint();
        } else if (change.equals("Update question")) {
            mainPanel.removeAll();
            mainPanel.repaint();
            Test test = (Test) pce.getNewValue();
            TestView testView = new TestView(); // skicka in testcontroll som en parameter
            mainPanel.add(testView);
            testView.setVariables(test);
            int CurrentPosition = test.getCurrentPosition();
            testView.setButtonColor(CurrentPosition);
            if (CurrentPosition + 1 == 20) {
                testView.setNextButton(1);
            } else {
                testView.setNextButton(0);
            }
        } else if (change.equals("Test over")) {
            mainPanel.removeAll();
            mainPanel.repaint();
            Test test = (Test) pce.getNewValue();
            ResultView resultView = new ResultView();
            mainPanel.add(resultView);
            resultView.setVariables(test);
            Pluggis.repaintViews();
        }
    }
}
